package com.tdam2014.grupo3.flickr.interfaces;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tdam2014.grupo3.entities.Photo;
import com.tdam2014.grupo3.flickr.FlickrException;
import com.tdam2014.grupo3.flickr.Parameter;
import com.tdam2014.grupo3.flickr.Response;
import com.tdam2014.grupo3.flickr.Transport;

/**
 * Interface for working with Flickr Photos.
 *
 * @author Anthony Eden, adapted by Lucas Cunibertti for Flickr Viewer.
 */
public class PhotoInterface {
	
	public static final String METHOD_GET_PHOTOS = "flickr.photosets.getPhotos";
    public static final String METHOD_GET_INFO = "flickr.photos.getInfo";

    private String apiKey;
    private Transport transport;

    public PhotoInterface(String apiKey, Transport transport) {
        this.apiKey = apiKey;
        this.transport = transport;
    }
    
    /**
	 * Get a collection of Photo objects for the specified PhotoSet.
	 * This method does not require authentication.
	 * 
	 * @param photoSetId The photoSet ID.
	 * @return a List of Photos objects.
	 * @throws IOException
	 * @throws FlickrException
	 * @throws JSONException
	 */
	public ArrayList<Photo> getPhotos(String photoSetId) throws IOException,
			FlickrException, JSONException {
		List<Parameter> parameters = new ArrayList<Parameter>();
		parameters.add(new Parameter("method", METHOD_GET_PHOTOS));
		parameters.add(new Parameter("api_key", apiKey));
		parameters.add(new Parameter("photoset_id", photoSetId));

		Response response = transport.get(transport.getPath(), parameters);
		if (response.isError()) {
			throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
		}

		JSONObject photoSetObject = response.getData().getJSONObject("photoset");
		JSONArray photoArray = photoSetObject.optJSONArray("photo");
		ArrayList<Photo> photos = new ArrayList<Photo>();

		for (int i = 0; photoArray != null && i < photoArray.length(); i++) {
			JSONObject photoObject = photoArray.getJSONObject(i);
			photos.add(getInfo(photoObject.getString("id")));
		}
		
		return photos;
	}
    
    /**
     * Get all info for the specified photo.
     * This method does not require authentication.
     *
     * @param photoId The photo Id.
     * @return a Photo object.
     * @throws IOException
     * @throws FlickrException
     * @throws JSONException 
     */
    public Photo getInfo(String photoId) throws IOException, FlickrException, JSONException {
        List<Parameter> parameters = new ArrayList<Parameter>();
        parameters.add(new Parameter("method", METHOD_GET_INFO));
        parameters.add(new Parameter("api_key", apiKey));
        parameters.add(new Parameter("photo_id", photoId));

        Response response = transport.get(transport.getPath(), parameters);
        if (response.isError()) {
            throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
        }
        
        JSONObject photoObject = response.getData().getJSONObject("photo");
        return new Photo(photoObject);
    }
}
